package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.AdmissionDto;
import xyz.zhouzhaodong.crm.dto.CostDto;
import xyz.zhouzhaodong.crm.entity.Admission;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.mapper.AdmissionMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.service.IAdmissionService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 录取表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-19
 */
@Service
public class AdmissionServiceImpl extends ServiceImpl<AdmissionMapper, Admission> implements IAdmissionService {

    @Resource
    AdmissionMapper admissionMapper;

    @Resource
    CustomerMapper customerMapper;

    String type = "补充材料";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addAdmission(AdmissionDto admissionDto) {
        try {
            Admission admission = admissionDtoToAdmission(admissionDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            admission.setId(id);
            admission.setCreateUser(admissionDto.getStaffId());
            admission.setCreateTime(new Date());
            admissionMapper.insert(admission);
            if(Objects.equals(admissionDto.getConnectType(), type)){
                // 需要更新客户状态
                Customer customer = new Customer();
                customer.setId(admissionDto.getCustomerId());
                customer.setConnectType(type);
                customerMapper.updateById(customer);
            }
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteAdmission(AdmissionDto admissionDto) {
        try {
            Admission admission = admissionDtoToAdmission(admissionDto);
            admission.setFlag(1);
            admission.setUpdateUser(admissionDto.getStaffId());
            admission.setUpdateTime(new Date());
            admissionMapper.updateById(admission);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateAdmission(AdmissionDto admissionDto) {
        try {
            Admission admission = admissionDtoToAdmission(admissionDto);
            admission.setUpdateUser(admissionDto.getStaffId());
            admission.setUpdateTime(new Date());
            admissionMapper.updateById(admission);
            if(Objects.equals(admissionDto.getConnectType(), type)){
                // 需要更新客户状态
                Customer customer = new Customer();
                customer.setId(admissionDto.getCustomerId());
                customer.setConnectType(type);
                customerMapper.updateById(customer);
            }
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectAdmission(AdmissionDto admissionDto) {
        QueryWrapper<Admission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", admissionDto.getCustomerId());
        if (admissionDto.getStaffId() == null) {
            admissionDto.setFlag(0);
        }
        List<Admission> list = admissionMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectAdmissionList(AdmissionDto admissionDto) {

        String staffId = admissionDto.getStaffId();
        Integer flag = admissionDto.getFlag();
        if(flag == null){
            flag = 0;
        }
        Page<AdmissionDto> page = new Page<>(admissionDto.getPageNo(),
                admissionDto.getPageSize() == 0 ? 10 : admissionDto.getPageSize());
        IPage<AdmissionDto> list = admissionMapper.selectAdmissionList(staffId, flag, page);
        for(AdmissionDto a : list.getRecords()){
            Customer customer = customerMapper.selectById(a.getCustomerId());
            a.setCustomer(customer);
        }
        return RestMessage.success(list);
    }

    private Admission admissionDtoToAdmission(AdmissionDto admissionDto) {
        Admission admission = new Admission();
        admission.setId(admissionDto.getId());
        admission.setCustomerId(admissionDto.getCustomerId());
        admission.setMaterialInformation(admissionDto.getMaterialInformation());
        admission.setQualified(admissionDto.getQualified());
        admission.setSupplementaryMaterials(admissionDto.getSupplementaryMaterials());
        admission.setRemark(admissionDto.getRemark());
        admission.setFlag(admissionDto.getFlag());
        return admission;
    }

}
